package com.ccxe.manage.web;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ccxe.manage.dao.impl.AdminLoginDaoImpl;
import com.ccxe.manage.filters.MdPwd;
import com.ccxe.manage.service.impl.MenuProcess;

/*
 *  页面登录
 */
public class LoginServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.doPost(req, resp);
	}

	@Override
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		if(req.getParameter("quit") != null){
			req.getSession().invalidate();
			req.getRequestDispatcher("/jsp/login.jsp").forward(req, resp);
			return;
		}
		

		PrintWriter out = resp.getWriter();
		AdminLoginDaoImpl lo = null;
		
		try {
			lo = new AdminLoginDaoImpl();
		} catch (Exception e) {
			e.printStackTrace();
		}
		String userName = null,mima,pass = null;
		try{
	    userName = req.getParameter("userName").trim();
	    pass = req.getParameter("password");
	    
//	    pass = MdPwd.getMD5(mima).trim();
		}catch(Exception e){
	    // userName = userName == null ? userName : userName.trim();
			req.getRequestDispatcher("/jsp/login.jsp").forward(req, resp);
			return;
		}

		boolean bool = lo.getLogin(userName, pass);

		if (bool) {
			req.getSession().setAttribute("userName", userName);
			req.getSession().setAttribute("rootMenu",
					new MenuProcess().getRootMenu());
			req.getRequestDispatcher("/jsp/index.jsp").forward(req, resp);
		} else {
			System.out.println("error login ");
			//JOptionPane.showMessageDialog(null, "用户名密码有误！");
			out.print("<script language='javascript'>alert('用户名密码有误！');window.location.href='/CCXIM/jsp/index.jsp';</script>");
			//req.getRequestDispatcher("/jsp/login.jsp").forward(req, resp);
		}
	}

}
